package com.pan.download5.factory.impl;

import com.pan.crawler.Page;
import com.pan.download5.factory.PageFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class CommonPageFactory implements PageFactory {

    private Class<? extends Page> pageClass;

    public CommonPageFactory(Class<? extends Page> pageClass) {
        this.pageClass = pageClass;
    }

    @Override
    public Page createPage() {

        Constructor<? extends Page> constructor = null;
        try {
            constructor = pageClass.getConstructor();
            Page page = constructor.newInstance();
            return page;
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
